/**
 * 初始化财务科目详情对话框
 */
var FeeInfoDlg = {
    feeInfoData : {},
    validateFields: {
        feeName: {
            validators: {
                notEmpty: {message: '名称不能为空'}
            }
        },
        paymentsType: {
            validators: {
                notEmpty: {message: '收支类型不能为空'}

            }
        },
        isWaybill: {
            validators: {
                notEmpty: {message: '运单相关不能为空'}
            }
        },
        useObject: {
            validators: {
                choice: {
                    min: 1,
                    message: '事务对象不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
FeeInfoDlg.clearData = function() {
    this.feeInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
FeeInfoDlg.set = function(key, val) {
    this.feeInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
FeeInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
FeeInfoDlg.close = function() {
    parent.layer.close(window.parent.Fee.layerIndex);
}

/**
 * 收集数据
 */
FeeInfoDlg.collectData = function() {
    this
    .set('id')
    .set('feeName')
    .set('feeType')
    .set('useObject')
    .set('isUse')
    .set('mark')
    .set('paymentsType')
    .set('deptId')
    .set('createTime')
    .set('updateTime')
    .set('createUserId')
    .set('isWaybill')
    ;
}

/**
 * 验证数据是否为空
 */
FeeInfoDlg.validate = function () {
    $('#feeInfoForm').data("bootstrapValidator").resetForm();
    $('#feeInfoForm').bootstrapValidator('validate');
    return $("#feeInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交添加
 */
FeeInfoDlg.addSubmit = function() {

    if (!this.validate()) {
        return;
    }

    var check_val="";
    $.each($('input:radio'),function(){
        if(this.checked){
            check_val+=$(this).val()+",";
        }
    });
    check_val=(check_val.substring(check_val.length-1)==',')?check_val.substring(0,check_val.length-1):check_val;
    $("#useObject").val(check_val);

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/fee/add", function(data){
        Feng.success("添加成功!");
        window.parent.Fee.table.refresh();
        FeeInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.feeInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
FeeInfoDlg.editSubmit = function() {

    if (!this.validate()) {
        return;
    }

    var check_val="";
    $.each($('input:radio'),function(){
        if(this.checked){
            check_val+=$(this).val()+",";
        }
    });
    check_val=(check_val.substring(check_val.length-1)==',')?check_val.substring(0,check_val.length-1):check_val;
    $("#useObject").val(check_val);

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/fee/update", function(data){
        Feng.success("修改成功!");
        window.parent.Fee.table.refresh();
        FeeInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.feeInfoData);
    ajax.start();
}

$(function() {
    Feng.initValidator("feeInfoForm", FeeInfoDlg.validateFields);

    $("#isUse").val($("#is_use").val());
    $("#paymentsType").val($("#payments_type").val());
    $("#isWaybill").val($("#is_waybill").val());

    if( typeof($("#id").val())!="undefined" ){
        var result=$("#use_object").val().split(",");
        for(var i=0;i<result.length;i++){
            $(":radio[value='"+result[i]+"']").attr("checked",true);
        }
    }
});
